package com.tjmc.tiomsresource.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-20 13:36
 */
public class CommonUtils {

    private static Logger logger = LoggerFactory.getLogger(CommonUtils.class);

    private static final String DEFAULT_DATETIME_STRING = "2000-01-01 00:00";
    private static final String DEFAULT_DATE_STRING = "2000-01-01";

    public static String utilDate2String(Date date, String format, boolean bDefaultDateShowBlank, String defaultShow) {
        DateFormat sdf = new SimpleDateFormat(format);
        String dateStr = sdf.format(date);
        if (bDefaultDateShowBlank && (dateStr.equals(DEFAULT_DATETIME_STRING) || dateStr.equals(DEFAULT_DATE_STRING))) {
            return defaultShow;
        }
        return dateStr;
    }

    public static String utilDate2String(Date date, String format, boolean bDefaultDateShowBlank) {
        DateFormat sdf = new SimpleDateFormat(format);
        String dateStr = sdf.format(date);
        if (bDefaultDateShowBlank && (dateStr.equals(DEFAULT_DATETIME_STRING) || dateStr.equals(DEFAULT_DATE_STRING))) {
            return "";
        }
        return dateStr;
    }

    public static String utilDate2String(Date date, boolean bDefaultDateShowBlank, String defaultShow) {
        return utilDate2String(date, "yyyy-MM-dd HH:mm", bDefaultDateShowBlank, defaultShow);
    }

    public static String utilDate2String(Date date, boolean bDefaultDateShowBlank) {
        return utilDate2String(date, "yyyy-MM-dd HH:mm", bDefaultDateShowBlank, "");
    }

    public static String utilDate2String(Date date, String defaultShow) {
        return utilDate2String(date, "yyyy-MM-dd HH:mm", true, defaultShow);
    }

    public static String utilDate2String(Date date) {
        return utilDate2String(date, "yyyy-MM-dd HH:mm", true, "");
    }


    public static Date string2utilDate(String value, String format) throws ParseException {
        DateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(value);
    }

    public static Date string2utilDate(String value) throws ParseException {
        return string2utilDate(value, "yyyy-MM-dd HH:mm");
    }

    public static Date restDate2utilDate(String value) throws ParseException {
        value = value.substring(0, value.length() - ".000Z".length()).replace('T', ' ');
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Calendar cal = Calendar.getInstance();
        Date date = sdf.parse(value);
//        cal.setTime(date);
//        cal.add(Calendar.HOUR_OF_DAY, 8);// before 8 hour
        return new Date(date.getTime() + 8 * 60 * 60 * 1000);
    }

    public static String utilDate2RestDate(Date date) {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(date);
//        cal.add(Calendar.HOUR_OF_DAY, -8);// before 8 hour
//        date = cal.getTime();
        date = new Date(date.getTime() - 8 * 60 * 60 * 1000);
        return sdf.format(date).replace(' ', 'T') + "Z";
    }

    public static boolean deleleFile(File filePath) {
        if (!filePath.exists()) {
            return false;
        }
        if (filePath.isDirectory()) {
            File[] files = filePath.listFiles();
            for (File f : files) {
                logger.info("deleleFile: " + f.getAbsolutePath());
                deleleFile(f);
            }
        }
        return filePath.delete();
    }

    public static boolean startsWithNumber(String str) {
        if(null == str || str.length() == 0) {
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str.charAt(0)+"");
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static boolean containsChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

}
